package com.winter.collection.List;

import java.util.*;

/**
 * @program: Collection
 * @description: LinkedList与ArrayList的性能差异
 * @author: Mr.Liang
 * @create: 2018-01-17 16:02
 **/
public class LinkedListVSArrayList {

    private static final int COUNT = 100000;
    private static LinkedList linkedList = new LinkedList();
    private static ArrayList arrayList = new ArrayList();
    private static Vector vector = new Vector();
    private static Stack stack = new Stack();

    /*获取集合的名字*/
    private static String getListName(List list) {
        if (list instanceof LinkedList) {
            return "LinkedList";
        } else if (list instanceof ArrayList) {
            return "ArrayList";
        } else if (list instanceof Vector) {
            return "Vector";
        } else if (list instanceof Stack) {
            return "Stack";
        } else {
            return "";
        }
    }

    public static void main(String[] args) {
        System.out.println("insert------------------------");
        insertByList(linkedList);
        insertByList(arrayList);
        insertByList(vector);
        insertByList(stack);
        System.out.println("-------------------------------");

    }

    /*向集合中插入数据*/
    private static void insertByList(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            list.add(0, i);
        }
        long endTime = System.currentTimeMillis();
        long value = startTime - endTime;
        System.out.println(getListName(list) + ":insert" + COUNT + "elements into the List use time:   " + value + "   ms");
    }


}
